﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DAO;
using BUS;

namespace AjaxChatService.Controllers
{
    [HttpHeader("Access-Control-Allow-Origin", "*")]
    public class RoomMembersController : ApiController
    {
        RoomMemberBUS roomMemberBUS = new RoomMemberBUS();

        public HttpResponseMessage PostRoomMember(RoomMember member)
        {
            if (ModelState.IsValid)
            {
                roomMemberBUS.AddRoomMember(member);
                RoomRequestBUS roomRequestBUS = new RoomRequestBUS();
                roomRequestBUS.DeleteRoomRequest(member.UserId.GetValueOrDefault(), member.RoomId.GetValueOrDefault());

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, member);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = member.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        public IEnumerable<ChatRoom> GetChatRooms(int userId)
        {
            var rooms = roomMemberBUS.GetChatRooms(userId);
            return rooms;
        }

        public HttpResponseMessage PostDeleteRoomMember(int userId)
        {
            if (roomMemberBUS.DeleteRoomMember(userId))
                return Request.CreateResponse(HttpStatusCode.OK);

            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }
}
